<template>
  <!-- 面试技巧 -->
  <div class="container">
    <div class="app-container">
      <!-- 顶部搜索 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="关键词">
          <el-input
            v-model="formInline.title"
            placeholder="根据文章标题搜索"
            @change="onSerch"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option
              v-for="item in elstate"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="inputClick">清空</el-button>
          <el-button type="primary" @click="onSerch">搜索</el-button>
        </el-form-item>

        <!-- 新增user -->
        <el-button
          class="addBtn"
          type="primary"
          icon="el-icon-edit"
          @click="dialogBtn('create')"
          >新增技巧</el-button
        >
      </el-form>

      <!-- 中间分隔 -->
      <div class="m_alert">
        <i class="el-icon-info"></i>
        <div class="m_text">
          数据一共<span>{{ total }}</span
          >条
        </div>
      </div>
      <!-- 放置table组件 -->
      <el-table
        v-loading="loading"
        :data="list"
        :model="form"
        :rules="rules"
        @close="closeDialog"
      >
        <!-- 放置列 -->
        <el-table-column type="index" align="center" width="80" label="序号" />
        <el-table-column prop="title" label="文章标题" width="300px">
          <template #default="{ row }">
            {{ row.title }}
            <i class="el-icon-film" v-if="row.videoURL" style="color: blue"
              ><video src="row.videoURL" style="width: 30px"></video
            ></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="visits"
          align="center"
          width="100"
          label="阅读人数"
        />
        <el-table-column
          prop="username"
          align="center"
          width="150"
          label="录入人"
        />
        <el-table-column
          prop="createTime"
          align="center"
          width="150"
          label="录入时间"
        />

        <el-table-column prop="state" align="center" width="100" label="状态">
          <template #default="{ row }">
            <span>{{ row.state ? '已启用' : '已禁用' }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template #default="{ row }">
            <el-button size="mini" type="text" @click="viewBtn(row)"
              >预览</el-button
            >
            <el-button
              v-if="row.state"
              size="mini"
              type="text"
              @click="changeState(row)"
              >禁用</el-button
            >
            <el-button v-else size="mini" type="text" @click="changeState(row)"
              >启用</el-button
            >
            <el-button
              :disabled="!row.state"
              size="mini"
              type="text"
              @click="editBtn('edit', row)"
              >修改</el-button
            >
            <el-button
              :disabled="!row.state"
              size="mini"
              type="text"
              @click="delBtn(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          background
          :current-page="quaryObj.page"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>

    <!-- dialog弹窗组件 -->
    <el-dialog width="70%" :title="dialogTitle" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item
          label="文章标题"
          prop="title"
          placeholder="请输入文章标题"
        >
          <el-input
            v-model="form.title"
            style="width: 100%"
            size="mini"
          />
        </el-form-item>
        <el-form-item
          label="文章内容"
          prop="articleBody"
          placeholder="请输入文章标题"
        >
          <quill-editor v-model="form.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" placeholder="请输入视频地址">
          <el-input
            v-model="form.videoURL"
            style="width: 100%"
            size="mini"
          />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="submitBtn"
                >确定</el-button
              >
              <el-button size="mini" @click="closeDialog">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 预览弹框组件 -->
    <viewDialog v-if='flag' :flag.sync='flag' :preView='preView'></viewDialog>
  </div>
</template>
<script>
// 富文本
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { list, add, detail, update, remove } from '@/api/hmmm/articles.js'
import { parseTime } from '@/utils/index.js'
import viewDialog from '../components/articles-dialog.vue'
export default {
  name: 'Role',
  components: {
    quillEditor,
    viewDialog
  },
  data () {
    return {
      flag: false,
      preView: {},
      options: [
        {
          value: '选项1',
          label: '黄金糕'
        },
        {
          value: '选项2',
          label: '双皮奶'
        }
      ],
      elstate: [
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      value: '',
      id: null,
      type: 'create',
      formInline: {
        title: '',
        state: ''
      },

      showDialog: false,
      loading: false,
      total: 0,
      quaryObj: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: ''
      },
      list: [],
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  created () {
    this.getDirectoryList()
  },

  methods: {
    // 发送获取列表信息
    async getDirectoryList () {
      this.loading = true
      const { data } = await list(this.quaryObj)
      this.list = data.items.filter(item => {
        item.createTime = parseTime(item.createTime)
        return true
      })

      this.list = [...data.items]
      this.total = data.counts
      this.loading = false
      // console.log(this.list)
    },
    // 搜索
    onSerch () {
      this.quaryObj.keyword = this.formInline.title
      this.quaryObj.state = this.formInline.state
      // console.log(this.quaryObj.keyword)
      this.getDirectoryList()
    },

    // 分页
    handleSizeChange (pageSize) {
      this.quaryObj.pagesize = pageSize
      // console.log(`每页 ${pageSize} 条`)
      this.quaryObj.page = 1

      this.getDirectoryList()
    },
    handleCurrentChange (current) {
      this.quaryObj.page = current
      // console.log(`当前页: ${current}`)
      this.getDirectoryList()
    },

    dialogBtn () {
      this.showDialog = true
    },
    // 预览
    viewBtn (row) {
      // console.log(1)
      this.flag = true
      this.preView = row
    },
    // 提交按钮
    async submitBtn () {
      try {
        if (this.type === 'create') {
          await add(this.form)
        }
        if (this.type === 'edit') {
          // console.log('edit')
          await update({ id: this.id, ...this.form })
        }
        this.getDirectoryList()
        this.closeDialog()
        this.$message.success(this.type === 'create' ? '创建成功' : '修改成功')
        // this.form.subjectName = ''
      } catch (error) {
        this.$message.error('创建失败')
      }
    },
    // 改变状态
    async changeState (row) {
      // const data = await changeState(row)
      // console.log(data)
      row.state = !row.state
      // this.disabled = !this.disabled
      // this.subDisabled = !this.subDisabled
    },
    // 修改按钮
    async editBtn (type, row) {
      this.type = type
      this.id = row.id
      const { data } = await detail(row)
      console.log(data)
      // this.form = { ...data }
      this.form.title = data.title
      this.form.articleBody = data.articleBody
      this.form.videoURL = data.videoURL
      // console.log(this.form)
      this.dialogBtn()
    },
    // 删除
    async delBtn (id) {
      // console.log(id)
      await remove(id)
      this.$message.success('删除成功')
      this.getDirectoryList()
    },

    closeDialog () {
      this.showDialog = false
      this.form.tagName = ''
      this.form.subjectID = ''
    },

    // 清空搜索框
    inputClick () {
      this.formInline.title = ''
      this.formInline.state = ''
      this.quaryObj.keyword = ''
      this.quaryObj.state = ''
      this.getDirectoryList()
    }
  },
  computed: {
    dialogTitle () {
      if (this.type === 'create') return '新增文章'
      if (this.type === 'edit') return '阅览文章'
      else {
        return '新增文章'
      }
    }
  }
}
</script>

<style scoped lang="less">
.addBtn {
  float: right;
  margin-left: 10px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  border: 1px solid #dcdfe6;
  font-weight: 500;
}
.role-operate {
  padding: 10px;
}

.m_alert {
  display: flex;
  background-color: #f4f4f5;
  color: #909399;
  width: 100%;
  padding: 8px 16px;
  margin-bottom: 15px;
  border-radius: 4px;
  background-color: #e5e5e8;
  i {
    display: inline-block;
    font-size: 16px;
    width: 16px;
    line-height: 1;
    margin-right: 5px;
  }
  .m-text {
    font-size: 13px;
    line-height: 18px;
  }
}
</style>
